﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Web.API.IBLL;
using Web.API.Model;

namespace Web.API.UI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StaffController : ControllerBase
    {
        public StaffController(StaffIBLL bll, ILogger<StaffController> logger) 
        {
            Bll = bll;
            Logger = logger;
        }
        public StaffIBLL Bll { get;}
        public ILogger<StaffController> Logger { get;}
        [HttpPost("AddStaff")]
        public Task<int> AddStaff(StaffModel staff)
        {
            try
            {
                Logger.LogInformation("添加成功");
                return Bll.AddStaff(staff);
            }
            catch (Exception)
            {
                throw;
            }
           
        }
        [HttpGet("GetStaff")]
        public dynamic GetStaff(string? name, int stafftypeid)
        {
            try
            {
                Logger.LogInformation("显示成功");
                return Bll.GetStaff(name, stafftypeid);
            }
            catch (Exception)
            {
                throw;
            }
           
        }
        [HttpGet("Xialak")]
        public List<StaffTypeModel> Xialak()
        {
            try
            {
                return Bll.Xialak();
            }
            catch (Exception)
            {
                throw;
            }
           
        }
        [HttpGet("ExportLevelExcel")]
        public IActionResult ExportLevelExcel()
        {
            var levellist = Bll.GetStaffModels();
            XSSFWorkbook sheets = new XSSFWorkbook();
            var sheet1 = sheets.CreateSheet("职员表");
            var row0 = sheet1.CreateRow(0);
            var c_0 = row0.CreateCell(0);
            c_0.SetCellValue("编号");
            var c_1 = row0.CreateCell(1);
            c_1.SetCellValue("姓名");
            var c_2 = row0.CreateCell(2);
            c_2.SetCellValue("性别");
            var c_3 = row0.CreateCell(3);
            c_3.SetCellValue("身份证号");
            var c_4 = row0.CreateCell(4);
            c_4.SetCellValue("手机号");
            var c_5 = row0.CreateCell(5);
            c_5.SetCellValue("职员类型");
            var c_6 = row0.CreateCell(6);
            c_6.SetCellValue("管理小区");


            int index = 1;
            foreach (var level in levellist)
            {
                var row = sheet1.CreateRow(index);
                var c0 = row.CreateCell(0);
                c0.SetCellValue(level.Id);

                var c1 = row.CreateCell(1);
                c1.SetCellValue(level.Name);

                var c2 = row.CreateCell(2);
                c2.SetCellValue(level.Sex);

                var c3 = row.CreateCell(3);
                c3.SetCellValue(level.StaffCard);
                var c4 = row.CreateCell(4);
                c4.SetCellValue(level.Phone);
                var c5 = row.CreateCell(5);
                c5.SetCellValue(level.StaffTypeId);
                var c6 = row.CreateCell(6);
                c6.SetCellValue(level.Addr);
                index++;



            }

            byte[] bs = null;
            using (MemoryStream ms = new MemoryStream())
            {
                sheets.Write(ms);
                bs = ms.ToArray();
            }
            return File(bs, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "职员信息表.xlsx");
        }
    }
}
